package com.lyc.service.impl;

import com.lyc.mapper.FollowMapper;
import com.lyc.mapper.UserMapper;
import com.lyc.pojo.UserFollows;
import com.lyc.pojo.User;
import com.lyc.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FollowServiceImpl implements FollowService {

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 关注操作 - 使用事务保证数据一致性
     */
    @Transactional
    @Override
    public void follow(Integer followerId, Integer followingId) {
        // 1. 检查是否已经关注（防止重复关注）
        if (followMapper.exists(followerId, followingId)) {
            throw new RuntimeException("已关注该用户");
        }

        // 2. 检查被关注用户是否存在
        User followingUser = userMapper.selectById(followingId);
        if (followingUser == null) {
            throw new RuntimeException("被关注用户不存在");
        }

        // 3. 检查关注者用户是否存在
        User followerUser = userMapper.selectById(followerId);
        if (followerUser == null) {
            throw new RuntimeException("关注者用户不存在");
        }

        // 4. 插入关注关系
        UserFollows userFollow = new UserFollows();
        userFollow.setFollowerId(followerId);
        userFollow.setFollowingId(followingId);
        followMapper.insert(userFollow);

        // 5. 更新关注者（follower）的 following_count
        User follower = userMapper.selectById(followerId);
        Integer followingCount = follower.getFollowingCount();
        if (followingCount == null) followingCount = 0;
        follower.setFollowingCount(followingCount + 1);
        userMapper.updateFollowingCount(follower);

        // 6. 更新被关注者（following）的 followers_count
        User following = userMapper.selectById(followingId);
        Integer followersCount = following.getFollowersCount();
        if (followersCount == null) followersCount = 0;
        following.setFollowersCount(followersCount + 1);
        userMapper.updateFollowersCount(following);
    }

    /**
     * 取消关注操作 - 使用事务保证数据一致性
     */
    @Transactional
    @Override
    public void unfollow(Integer followerId, Integer followingId) {
        // 1. 检查是否已关注
        if (!followMapper.exists(followerId, followingId)) {
            throw new RuntimeException("未关注该用户");
        }

        // 2. 检查被关注用户是否存在
        User followingUser = userMapper.selectById(followingId);
        if (followingUser == null) {
            throw new RuntimeException("被关注用户不存在");
        }

        // 3. 检查关注者用户是否存在
        User followerUser = userMapper.selectById(followerId);
        if (followerUser == null) {
            throw new RuntimeException("关注者用户不存在");
        }

        // 4. 删除关注关系
        followMapper.deleteByFollowerAndFollowing(followerId, followingId);

        // 5. 更新关注者（follower）的 following_count
        User follower = userMapper.selectById(followerId);
        Integer followingCount = follower.getFollowingCount();
        if (followingCount == null) followingCount = 0;
        follower.setFollowingCount(followingCount - 1);
        userMapper.updateFollowingCount(follower);

        // 6. 更新被关注者（following）的 followers_count
        User following = userMapper.selectById(followingId);
        Integer followersCount = following.getFollowersCount();
        if (followersCount == null) followersCount = 0;
        following.setFollowersCount(followersCount - 1);
        userMapper.updateFollowersCount(following);
    }

    // 获取用户关注列表
    @Override
    public List<UserFollows> getFollowingList(Integer userId) {
        return followMapper.selectFollowingList(userId);
    }

    // 获取用户粉丝列表
    @Override
    public List<UserFollows> getFollowerList(Integer userId) {
        return followMapper.selectFollowerList(userId);
    }
}
